package com.yanlight.app.controller;

import com.yanlight.Constants;
import com.yanlight.app.dal.entity.AdCount;
import com.yanlight.app.dal.entity.AwardSimpleItem;
import com.yanlight.app.service.AdCountService;
import com.yanlight.exception.ErrorType;
import com.yanlight.support.http.Response;
import com.yanlight.util.DrawLotteryUtil;
import com.yanlight.util.IpUtil;
import com.yanlight.util.LogUtil;
import com.yanlight.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/ad")
public class AdController {

    @Autowired
    AdCountService adCountService;


    @RequestMapping("/wheelView")
    public ModelAndView wheelView(ModelAndView model, String authId, Integer type, String codeValue, String companyId) {

        model.setViewName(Constants.ROTATE_WHEEL);
        model.addObject("authId", authId);
        model.addObject("type", type);
        model.addObject("codeValue", codeValue);
        model.addObject("companyId", companyId);
        return model;
    }

    @PostMapping("/enableTimes")
    @ResponseBody
    public Response enableTimes(String authId) {
        Integer times = adCountService.todayAwardTimes(authId);
        LogUtil.showLog("enableTimes", times);
        return ResponseUtil.success(times);
    }


    @PostMapping("/clickCount")
    @ResponseBody
    public Response clickCount(HttpServletRequest request, AdCount adCount) {
        String requestIp = IpUtil.getIP(request);
        adCount.setIp(requestIp);

        Response response = adCountService.clickCount(adCount);
        LogUtil.showLog("clickCount", response);
        return response;
    }

    @PostMapping("/awardList")
    @ResponseBody
    public Response awardList() {
        List<AwardSimpleItem> simpleAwardList = DrawLotteryUtil.getSimpleAwardList();
        if (simpleAwardList == null || simpleAwardList.isEmpty()) {
            return ResponseUtil.fail(ErrorType.AWARD_LIST_FAIL);
        } else {
            return ResponseUtil.success(simpleAwardList);
        }
    }

    @PostMapping("/awardResult")
    @ResponseBody
    public Response awardResult(String authId, Integer type) {
        Integer result = adCountService.getResult(authId, type);
        if (result == -1) {
            return ResponseUtil.fail(ErrorType.AWARD_FAIL);
        } else {
            //计算出抽奖结果
            return ResponseUtil.success(result);
        }
    }

    @PostMapping("/redPacket")
    @ResponseBody
    public Response redPacket(Integer type, String authId, Long companyId, String codeValue) {
//        if (type == null || StringUtils.isBlank(authId) || companyId == null || StringUtils.isBlank(codeValue)) {
//            return ResponseUtil.fail(ErrorType.INCOMPLETE_PARAMETERS);
//        }
//        Response response = adCountService.verifyAward(type, authId, companyId, codeValue);
//        return response;
        //        WxRedPacketRequest request = new WxRedPacketRequest();
//        String nonceStr = RandomStringUtil.randomAlphanumeric(32);
//        String nowTimeNoChar = DateTimeUtil.getNowTimeNoChar();
//
//        String orderNumber = WxConstants.mchId + nowTimeNoChar + RandomStringUtil.randomNumeric(4);
//        WxRedPacketRequest request = new WxRedPacketRequest();
//
//        request.setMch_billno(orderNumber);
//        request.setNonce_str(nonceStr);
//        request.setWxappid(WxConstants.appId);
//        request.setMch_id(WxConstants.mchId);
//        request.setSend_name("微食刻");
//        request.setRe_openid("oEMmQ5lmo4MzAnLuT3pRlgEFecHY");
//        request.setTotal_amount(100);
//        request.setTotal_num(1);
//        request.setWishing("感谢您的支持！");
//        request.setClient_ip("39.98.40.103");
//        request.setAct_name("测试红包");
//        request.setRemark("先到先得哦");
//        request.setScene_id("PRODUCT_1");
//
//        SortedMap<String, String> stringStringSortedMap = request.setupParams();
//        String apiSignHMAC = SignatureUtil.createApiSign(stringStringSortedMap, WxConstants.apiKey);
//        request.setSign(apiSignHMAC);
//        String xmlFromObj = XmlParseUtil.getXmlFromObj(request);
//
//        String result = WxHttpClientUtil.xmlPost(WxConstants.WX_RED_PACKET, WxConstants.mchId, WxConstants.certPath, xmlFromObj);
//        Map<String, String> stringStringMap = XmlUtil.xmlToMap(result);

        return ResponseUtil.success();
    }


    @PostMapping("/testSendRedPacket")
    @ResponseBody
    public Response testSendRedPacket() {
//        WxRedPacketRequest request = new WxRedPacketRequest();
//        String nonceStr = RandomStringUtil.randomAlphanumeric(32);
//        String nowTimeNoChar = DateTimeUtil.getNowTimeNoChar();
//
//        String orderNumber = WxConstants.mchId + nowTimeNoChar + RandomStringUtil.randomNumeric(4);
//
//        request.setMch_billno(orderNumber);
//        request.setNonce_str(nonceStr);
//        request.setWxappid(WxConstants.appId);
//        request.setMch_id(WxConstants.mchId);
//        request.setSend_name("微食刻");
//        request.setRe_openid("oEMmQ5lmo4MzAnLuT3pRlgEFecHY");
//        request.setTotal_amount(1);
//        request.setTotal_num(1);
//        request.setWishing("感谢您的支持！");
//        request.setClient_ip("39.98.40.103");
//        request.setAct_name("测试红包");
//        request.setRemark("先到先得哦");
//        request.setScene_id("PRODUCT_2");
//
//        SortedMap<String, String> stringStringSortedMap = request.setupParams();
//        String apiSignHMAC = SignatureUtil.createApiSign(stringStringSortedMap, WxConstants.apiKey);
//        request.setSign(apiSignHMAC);
//        String xmlFromObj = XmlParseUtil.getXmlFromObj(request);
//
//        String result = WxHttpClientUtil.xmlPost(WxConstants.WX_RED_PACKET, WxConstants.mchId, WxConstants.certPath, xmlFromObj);
//        Map<String, String> stringStringMap = XmlUtil.xmlToMap(result);
//
//        return ResponseUtil.success(stringStringMap);
        LogUtil.showLog("测试", "你哈哈哈哈");
        return ResponseUtil.success();
    }
}
